#!/bin/sh

# Purpose			: Shell script is example to retrieve a variable value inside a background while loop
#					  Its not possible by just running first loop in background because and possible to to access variable from other shell.
#					  Hense the other way is through piping of first loop
#					  Here store dynamic variable in tmp file and read that tmp file from the other loop.
# Date/Time         : Wed Mar 14 11:41:22 IST 2012
# Modification Date :

write_var()
{
	echo ${VAR} > ${TMPFILE}
}

TMPFILE=$(mktemp)

trap "rm -f $tmp; exit 1" 0 1 2 3 13 15

while true; do
	sleep 3
	VAR=BBB
	write_var
	sleep 3
	VAR=CCC
	write_var
done &

VAR=AAA
write_var

while true; do
	cat ${TMPFILE}
	sleep 1
done 

rm -f ${TMPILE}
